package com.masterclient.app.modules.cart.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.masterclient.app.modules.cart.beans.ProductsItemBean;
import com.masterclient.app.modules.cart.beans.PromotionsItemBean;
import com.masterclient.app.modules.cart.beans.SellerItemBean;
import com.masterclient.app.modules.cart.beans.ShopCartCustomItemBean;
import com.masterclient.app.modules.cart.holder.ActivityHeadHolder;
import com.masterclient.app.modules.cart.holder.DisableHeadHolder;
import com.masterclient.app.modules.cart.holder.DisableItemHolder;
import com.masterclient.app.modules.cart.holder.GoodsItemHolder;
import com.masterclient.app.modules.cart.holder.NullViewHolder;
import com.masterclient.app.modules.cart.holder.RecommendHolder;
import com.masterclient.app.modules.cart.holder.ShopHeadHolder;
import com.eoner.commonbean.product.ProductMessage;

import java.util.ArrayList;
import java.util.List;

public class ShopCartAdapter extends RecyclerView.Adapter {

    /**
     * 店铺头
     */
    private final int SHOP_VIEW = 1;

    /**
     * 满减活动
     */
    private final int ACTIVITY_VIEW = 7;

    /**
     * 商品内容
     */
    private final int GOODS_VIEW = 2;

    /**
     * 失效标题
     */
    private final int DISABLED_HEAD_VIEW = 3;

    /**
     * 失效商品
     */
    private final int DISABLED_VIEW = 4;

    /**
     * 推荐商品
     */
    private final int RECOMMEND_VIEW = 5;

    /**
     * 空
     */
    private final int NULL_VIEW = 6;

    private ShopCartAdapterListener mListener;
    private List<ShopCartCustomItemBean> shopCartCustomItemBeanList = new ArrayList<>();

    public ShopCartAdapter(ShopCartAdapterListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (ShopCartCustomItemBean.SHOP.equals(shopCartCustomItemBeanList.get(position).type)) {
            return SHOP_VIEW;
        } else if (ShopCartCustomItemBean.ACTIVITY.equals(shopCartCustomItemBeanList.get(position).type)) {
            return ACTIVITY_VIEW;
        } else if (ShopCartCustomItemBean.GOODS.equals(shopCartCustomItemBeanList.get(position).type)) {
            return GOODS_VIEW;
        } else if (ShopCartCustomItemBean.DISABLED_HEAD.equals(shopCartCustomItemBeanList.get(position).type)) {
            return DISABLED_HEAD_VIEW;
        } else if (ShopCartCustomItemBean.DISABLED.equals(shopCartCustomItemBeanList.get(position).type)) {
            return DISABLED_VIEW;
        } else if (ShopCartCustomItemBean.RECOMMEND.equals(shopCartCustomItemBeanList.get(position).type)) {
            return RECOMMEND_VIEW;
        } else {
            return NULL_VIEW;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == SHOP_VIEW) {
            return new ShopHeadHolder(parent, mListener);
        } else if (viewType == ACTIVITY_VIEW) {
            return new ActivityHeadHolder(parent, mListener);
        } else if (viewType == GOODS_VIEW) {
            return new GoodsItemHolder(parent, mListener);
        } else if (viewType == DISABLED_HEAD_VIEW) {
            return new DisableHeadHolder(parent, mListener);
        } else if (viewType == DISABLED_VIEW) {
            return new DisableItemHolder(parent, mListener);
        } else if (viewType == RECOMMEND_VIEW) {
            return new RecommendHolder(parent);
        } else {
            return new NullViewHolder(parent, mListener);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == SHOP_VIEW) {
            ShopHeadHolder shopHeadHolder = (ShopHeadHolder) holder;
            SellerItemBean itemData = (SellerItemBean) shopCartCustomItemBeanList.get(position).itemData;
            shopHeadHolder.onBindViewHolder(itemData, shopCartCustomItemBeanList.get(position).originalPosition);
        } else if (getItemViewType(position) == ACTIVITY_VIEW) {
            ActivityHeadHolder activityHeadHolder = (ActivityHeadHolder) holder;
            PromotionsItemBean promotionsItemBean = (PromotionsItemBean) shopCartCustomItemBeanList.get(position).itemData;
            activityHeadHolder.onBindViewHolder(promotionsItemBean);
        } else if (getItemViewType(position) == GOODS_VIEW) {
            GoodsItemHolder goodsItemHolder = (GoodsItemHolder) holder;
            ProductsItemBean itemData = (ProductsItemBean) shopCartCustomItemBeanList.get(position).itemData;
            goodsItemHolder.onBindViewHolder(itemData, shopCartCustomItemBeanList.get(position).originalPosition);
        } else if (getItemViewType(position) == DISABLED_HEAD_VIEW) {
            DisableHeadHolder disableHeadHolder = (DisableHeadHolder) holder;
            disableHeadHolder.onBindViewHolder(shopCartCustomItemBeanList.get(position));
        } else if (getItemViewType(position) == DISABLED_VIEW) {
            DisableItemHolder disableItemHolder = (DisableItemHolder) holder;
            ProductsItemBean itemData = (ProductsItemBean) shopCartCustomItemBeanList.get(position).itemData;
            disableItemHolder.onBindViewHolder(itemData, shopCartCustomItemBeanList.get(position).originalPosition);
        } else if (getItemViewType(position) == RECOMMEND_VIEW) {
            RecommendHolder recommendHolder = (RecommendHolder) holder;
            List<ProductMessage> itemData = (List<ProductMessage>) shopCartCustomItemBeanList.get(position).itemData;
            recommendHolder.onBindViewHolder(itemData);
        } else {
            NullViewHolder nullViewHolder = (NullViewHolder) holder;
            nullViewHolder.onBindViewHolder();
        }
    }

    @Override
    public int getItemCount() {
        return shopCartCustomItemBeanList.size();
    }

    public void setData(List<ShopCartCustomItemBean> shopCartCustomItemBeanLis) {
        this.shopCartCustomItemBeanList = shopCartCustomItemBeanLis;
        notifyDataSetChanged();
    }
}
